package com.hs.framework.utils

import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.TextStyle
import java.time.temporal.ChronoUnit
import java.util.Calendar
import java.util.Date
import java.util.Locale

object DateUtils {
    // 常用日期时间格式
    private const val DEFAULT_DATE_FORMAT = "yyyy-MM-dd"
    private const val DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"

    private const val DEFAULT_DATE_FORMAT_YYMMDD_CHINA = "yyyy年-MM月-dd日"

    private const val DEFAULT_DATETIME_FORMAT_HHMM = "HH:mm"

    // 格式化日期（使用java.util.Date类型）为指定格式的字符串
    fun formatDate(date: Date, pattern: String = DEFAULT_DATE_FORMAT): String {
        val sdf = SimpleDateFormat(pattern, Locale.getDefault())
        return sdf.format(date)
    }

    // 格式化LocalDateTime为指定格式的字符串
    fun formatLocalDateTime(
        dateTime: LocalDateTime,
        pattern: String = DEFAULT_DATETIME_FORMAT
    ): String {
        val formatter = DateTimeFormatter.ofPattern(pattern)
        return dateTime.format(formatter)
    }

    // 计算两个LocalDate之间的天数间隔
    fun daysBetween(startDate: LocalDate, endDate: LocalDate): Long {
        return ChronoUnit.DAYS.between(startDate, endDate)
    }

    // 给LocalDate增加指定天数
    fun addDays(date: LocalDate, days: Long): LocalDate {
        return date.plusDays(days)
    }

    // 将java.util.Date类型的日期转换为时间戳（毫秒）
    fun dateToTimestamp(date: Date): Long {
        return date.time
    }

    // 将Instant转换为时间戳（毫秒）
    fun instantToTimestamp(instant: Instant): Long {
        return instant.toEpochMilli()
    }

    // 将时间戳（毫秒）转换为java.util.Date类型的日期
    fun timestampToDate(timestamp: Long): Date {
        return Date(timestamp)
    }

    // 将时间戳（毫秒）转换为LocalDateTime
    fun timestampToLocalDateTime(timestamp: Long): LocalDateTime {
        val instant = Instant.ofEpochMilli(timestamp)
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
    }

    // 获取当前的java.util.Date类型的日期
    fun getCurrentDate(): Date {
        return Date()
    }

    // 获取当前的LocalDateTime
    fun getCurrentLocalDateTime(): LocalDateTime {
        return LocalDateTime.now()
    }

    // 比较两个LocalDate的先后顺序，返回 -1表示date1早于date2，0表示相等，1表示date1晚于date2
    fun compareLocalDates(date1: LocalDate, date2: LocalDate): Int {
        return date1.compareTo(date2)
    }

    fun formatDateHHMMSS(date: Date): String {
        val sdf = SimpleDateFormat(DEFAULT_DATETIME_FORMAT, Locale.getDefault())
        return sdf.format(date)
    }

    fun formatDateHHMM(date: Date): String {
        val sdf = SimpleDateFormat(DEFAULT_DATETIME_FORMAT_HHMM, Locale.getDefault())
        return sdf.format(date)
    }

    fun getYYMMDD(): String {
        val sdf = SimpleDateFormat(DEFAULT_DATE_FORMAT_YYMMDD_CHINA, Locale.getDefault())
        val localDate = Date()
        return "${sdf.format(localDate)} ${getDayOfWeekUsingLocalDate()}"
    }


    fun getDayOfWeekUsingCalendar(): String {
        val calendar = Calendar.getInstance()
        return getDayOfWeek(calendar)
    }

    fun getDayOfWeek(date: Date): String {
        val calendar = Calendar.getInstance()
        calendar.time = date
        return getDayOfWeek(calendar)
    }

    fun getDayOfWeek(calendar: Calendar): String {
        val dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
        return when (dayOfWeek) {
            Calendar.SUNDAY -> "星期日"
            Calendar.MONDAY -> "星期一"
            Calendar.TUESDAY -> "星期二"
            Calendar.WEDNESDAY -> "星期三"
            Calendar.THURSDAY -> "星期四"
            Calendar.FRIDAY -> "星期五"
            Calendar.SATURDAY -> "星期六"
            else -> "无效的日期"
        }
    }

    fun getDayOfWeekUsingLocalDate(): String {
        val localDate = LocalDate.now()
        val dayOfWeek = localDate.getDayOfWeek()
        val dayName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA)
        return dayName
    }

}